package com.guohe3.just.service.impl;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import com.guohe3.just.DO.Tiyu;
import com.guohe3.just.service.TiyuService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class TiyuServiceImpl implements TiyuService {
    public List<Tiyu> result(String username, String Vpnpassword, String Tiyupassword){
        List<Tiyu> list = new ArrayList<>();
        final WebClient webClient = new WebClient(BrowserVersion.CHROME);//新建一个模拟谷歌Chrome浏览器的浏览器客户端对象
        webClient.getOptions().setThrowExceptionOnScriptError(false);//当JS执行出错的时候是否抛出异常, 这里选择不需要
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常, 这里选择不需要
        webClient.getOptions().setActiveXNative(false);
        webClient.getOptions().setCssEnabled(false);//是否启用CSS, 因为不需要展现页面, 所以不需要启用
        webClient.getOptions().setJavaScriptEnabled(true); //很重要，启用JS
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要，设置支持AJAX
        webClient.getOptions().setUseInsecureSSL(true);
        HtmlPage page = null;
        HtmlPage page2 = null;
        HtmlPage tiyu = null;
        HtmlPage tiyu2 = null;
        HtmlPage tiyu3 = null;
        try {
            page = webClient.getPage("https://vpn.just.edu.cn/dana-na/auth/url_default/welcome.cgi");//尝试加载上面图片例子给出的网页
            HtmlForm form = page.getFormByName("frmLogin");
            HtmlTextInput formusername = form.getInputByName("username");
            HtmlPasswordInput password = form.getInputByName("password");
            HtmlSubmitInput button = form.getInputByName("btnSubmit");
            formusername.setValueAttribute(username);
            password.setValueAttribute(Vpnpassword);
            page2 = button.click();
            if(page2.asXml().contains("There are already other user sessions in progress")) {
                form = page2.getFormByName("frmConfirmation");
                HtmlSubmitInput button2 = form.getInputByName("btnContinue");
                page2=button2.click();
            };
            tiyu = webClient.getPage("https://vpn.just.edu.cn/,DanaInfo=202.195.195.147+login.asp");
            HtmlForm form2 = tiyu.getFormByName("myform");
            formusername = form2.getInputByName("username");
            password = form2.getInputByName("password");
            HtmlButtonInput button2 = form2.getInputByName("Submit");
            formusername.setValueAttribute(username);
            password.setValueAttribute(Tiyupassword);
            tiyu2 = button2.click();
            tiyu3 = webClient.getPage("https://vpn.just.edu.cn/,DanaInfo=202.195.195.147/xsgl/cjcx.asp");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            webClient.close();
        }

        //异步JS执行需要耗时,所以这里线程要阻塞30秒,等待异步JS执行结束

        String pageXml = tiyu3.asXml();//直接将加载完成的页面转换成xml格式的字符串
        Document document = Jsoup.parse(pageXml);
        Elements element = document.select("tr[align=center]");

        for(int i=1;i<element.size();i++) {
            Tiyu tiyuinfo = new Tiyu();
            Elements newElements = element.get(i).select("nobr");
            int p =0;
            tiyuinfo.setXueqi(newElements.get(p++).text());
            tiyuinfo.setZhuanxiang(newElements.get(p++).text());
            tiyuinfo.setShangkeshijian(newElements.get(p++).text());
            tiyuinfo.setTiyujiaoshi(newElements.get(p++).text());
            tiyuinfo.setKechengming(newElements.get(p++).text());
            tiyuinfo.setLevel(newElements.get(p++).text());
            tiyuinfo.setBeizhu(newElements.get(p++).text());
            tiyuinfo.setGrad(element.get(i).select("td").get(5).text());
            list.add(tiyuinfo);
        }
        return list;
    };
}
